/*************************************************************************
	> File Name: composite.h
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: 2021年12月02日 星期四 21时00分22秒
 ************************************************************************/

#include<iostream>
#include <string>
#include <unordered_set>

class Component
{
public:
	Component(std::string n) : name(n) {}

	virtual void add(Component* c) = 0;
	virtual void remove(Component* c) = 0;
	virtual void display(int depth) = 0;

protected:
	std::string name;
};

class Leaf : public Component
{
public:
	using Component::Component;

	virtual void add(Component* c)
	{
		std::cout << "无法增加一个叶节点" << std::endl;
	}

	virtual void remove(Component* c)
	{
		std::cout << "无法删除一个叶节点" << std::endl;
	}

	virtual void display(int depth)
	{
		std::cout << std::string(depth, '-') << name << std::endl;
	}
};

class Composite : public Component
{
public:
	using Component::Component;

	virtual void add(Component* c)
	{
		children.insert(c);
	}

	virtual void remove(Component* c)
	{
		children.erase(c);
	}

	virtual void display(int depth)
	{
		std::cout << std::string(depth, '-') << name << std::endl;
		for (auto elem : children)
			elem->display(depth + 2);
	}

private:
	std::unordered_set<Component*> children;
};

